#include "loadform.h"
#include "ui_loadform.h"
#include<QDebug>
#include"clientSock.h"
#include"agreement.h"

loadForm::loadForm(int id,std::string fileIdenter,std::string filename,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loadForm)
{
    ui->setupUi(this);
    windowId = id;
    fileName = filename;
    m_fileIdenter = fileIdenter;
//    qDebug()<<QString::fromStdString(fileIdenter)<<"\t"<<QString::fromStdString(m_fileIdenter);
    ui->fileName->setText(QString::fromStdString(fileName));
    ui->progressBar->setValue(0);
}

loadForm::~loadForm()
{
    delete ui;
}

void loadForm::setProgressBar(std::string fileIdenter, int per)
{
//    qDebug()<<QString::fromStdString(fileIdenter)<<"\t"<<QString::fromStdString(m_fileIdenter)<<"\t"<<per;
    if(fileIdenter == m_fileIdenter){
        int value = std::max(per,ui->progressBar->value());
        ui->progressBar->setValue(value);
    }
    if(per == 100 && fileIdenter == m_fileIdenter){
        close();
    }
}


void loadForm::on_pauseButton_clicked()
{
    if(m_fileIdenter.empty()) return;
    CloudDrive::Packet pkt = CloudDrive::makePacket(CloudDrive::MessageType::DOWNFILE_PAUSE);
    CloudDrive::FileInfo fi;
    std::strncpy(fi.fileIdentfir,m_fileIdenter.c_str(),CloudDrive::MAX_FILEIDENTER_LEN-1);
    qDebug()<<QString::fromStdString(fi.fileIdentfir);
    Client::getInstance().sendData(CloudDrive::makeFileInfoPacket(pkt,fi,4));
}

void loadForm::on_continueButton_clicked()
{
    if(m_fileIdenter.empty()) return;
    CloudDrive::Packet pkt = CloudDrive::makePacket(CloudDrive::MessageType::DOWNFILE_CONTINUE);
    CloudDrive::FileInfo fi;
    std::strncpy(fi.fileIdentfir,m_fileIdenter.c_str(),CloudDrive::MAX_FILEIDENTER_LEN-1);
    Client::getInstance().sendData(CloudDrive::makeFileInfoPacket(pkt,fi,4));
}

void loadForm::on_cancelButton_clicked()
{
    if(m_fileIdenter.empty()) return;
    CloudDrive::Packet pkt = CloudDrive::makePacket(CloudDrive::MessageType::DOWNFILE_CANCEL);
    CloudDrive::FileInfo fi;
    std::strncpy(fi.fileIdentfir,m_fileIdenter.c_str(),CloudDrive::MAX_FILEIDENTER_LEN-1);
    Client::getInstance().sendData(CloudDrive::makeFileInfoPacket(pkt,fi,4));
}
